package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.AdWmChannelMapper;
import com.heima.wemedia.service.AdWmChannelService;
import com.heima.wemedia.service.WmNewsService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author lzq
 * @data 2023/5/9 16:24
 */
@Service
@Slf4j
public class AdWmChannelServiceImpl extends ServiceImpl<AdWmChannelMapper, WmChannel> implements AdWmChannelService {

    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 频道列表查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(ChannelDto dto) {
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //参数校验
        dto.checkParam();
        //分页构造器
        Page<WmChannel> page = new Page<>(dto.getPage(),dto.getSize());
        //条件构造器
        LambdaQueryWrapper<WmChannel> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(dto.getName()),WmChannel::getName,dto.getName());
        lqw.eq(dto.getStatus()!=null,WmChannel::getStatus,dto.getStatus());
        lqw.orderByDesc(WmChannel::getCreatedTime);
        page(page,lqw);
        //封装数据
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());

        return responseResult;
    }

    /**
     * 删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult del(Integer id) {
        //参数校验
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询频道
        WmChannel wmChannel = getById(id);
        if (wmChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //判断是否为禁用
        if (wmChannel.getStatus()==true){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_User,"频道被启用状态无法删除");
        }
        //不是禁用,删除
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 新增频道
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {
        //数据校验
        if (wmChannel==null|| StringUtils.isBlank(wmChannel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询频道名称是否重复
        WmChannel channel = getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, wmChannel.getName()));
        if (channel!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //保存
        wmChannel.setCreatedTime(new Date());
        wmChannel.setIsDefault(true);
        save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改频道
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {
        //数据校验
        if (wmChannel==null|| StringUtils.isBlank(wmChannel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询文章是否被引用
        List<WmNews> wmNewsList = wmNewsService.list(Wrappers.<WmNews>lambdaQuery()
                .eq(WmNews::getChannelId, wmChannel.getId()));
        if (wmNewsList!=null&&wmNewsList.size()>0){
            if (wmChannel.getStatus()==false){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_User,"频道被文章引用,无法删除");
            }
        }
        wmChannel.setIsDefault(true);
        wmChannel.setCreatedTime(new Date());
        updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
